Ext.define('Lms.rms.SystemMgmPanel',{
	extend : 'Ext.grid.Panel',
	initComponent : function(){
		Ext.define('PcModel', {
	        extend: 'Ext.data.Model',	        
	        fields: [
	            'id', 'name', 'zone', 'zoneName', 'type', 
	            'ip', 'macAddress', 'description', 'hwState', 'swState', 'state',
	            'lifeTime', 'projectorLifeTime'
	        ],
	        idProperty: 'id'
	    });
		
		var store = Ext.create('Ext.data.Store',{
			model : 'PcModel',
			sortInfo: {
		        field    : 'name',
		        direction: 'ASC'
		    },
			proxy : {
				type : 'ajax',
				url : 'pc/pageList.do',
				actionMethods : {
					read : 'POST'					
				},
				reader : {
					type : 'json',
					root : 'list',
	                totalProperty : 'totalCount'
				}
			}
		});
		
		Ext.apply(this,{
			id : 'SystemMgmPanel',
			title : LocaleUtil.getMessage('rms.menu.systemMng'),
			store : store,
			selType: 'checkboxmodel',
			columns : this.getColumns(),
			viewConfig : Common.viewConfig(true),
			columnLines: true,
			bbar: Common.pagingToolBar(store,400),
			tbar : [{
				xtype : 'label',
				text : LocaleUtil.getMessage('rms.label.category')
			},{
				xtype : 'combobox',
				itemId : 'searchCategory',
				emptyText : 'All',
				store : [['','All'],['ZONE',LocaleUtil.getMessage('rms.label.zone')],['NAME',LocaleUtil.getMessage('rms.label.systemName')]],
				forceSelection: true,
	          	editable: false,
				value : ''
			},{
				xtype : 'textfield',
				itemId : 'searchWordTxt',
				width : 200,
				enableKeyEvents : true,
				maskRe: Common.getMakeRe(),
				listeners : {
					scope : this,
					keypress : function(txtField, e, eOpts){
						if(e.getKey() == 13){
							this.search();
						}
					}
				}
			},{
				xtype : 'button',
				iconCls : 'detail_search',
				text : LocaleUtil.getMessage('search'),
				scope : this,
				handler : this.search
			},'->',{
				xtype : 'button',
				text : LocaleUtil.getMessage('reg'),
				iconCls : 'add',
				handler : function(){
					Ext.create('Lms.rms.SystemInfoDialog').openWindow();
				}
			},{
				xtype : 'button',
				text : LocaleUtil.getMessage('del'),
				iconCls : 'delete',
				scope : this,
				handler : this.onDeleteHandler
			}]
		});
		this.callParent(arguments);
		this.on('celldblclick',this.onCellClickHandler);
		this.on('show',function(){
			store.load();
		},this);
	},
	
	getColumns : function(){
		return [
		    {text : LocaleUtil.getMessage('rms.label.zone'),dataIndex : 'zoneName'},
		    {text : LocaleUtil.getMessage('rms.label.systemName'),dataIndex : 'name', flex:1},
		    {text : LocaleUtil.getMessage('rms.label.lifeTime'),dataIndex : 'lifeTime', width: 80},
		    {text : LocaleUtil.getMessage('rms.label.projectorLifeTime'),dataIndex : 'projectorLifeTime', width: 120},
		    {text : LocaleUtil.getMessage('rms.label.ipAddress'),dataIndex : 'ip', width: 120},
		    {text : LocaleUtil.getMessage('rms.label.macAddress'),dataIndex : 'macAddress', width: 120},
		    {text : LocaleUtil.getMessage('status'), dataIndex : 'state', width: 50,renderer : this.getStateName}
	    ];
	},
	
	getStateName : function(value){
		if(value != 'OKE'){
			return 'FAIL';
		}else{
			return 'OK';
		}
	},
	
	onCellClickHandler : function(grid, td, cellIndex, record, tr, rowIndex, e, eOpts){
		if(cellIndex == 0) return true;
		if(cellIndex == 1){
			Ext.create('Lms.rms.SystemInfoDialog').openWindow({id : record.get('id')});
		}
	},
	
	onDeleteHandler : function(){
		var selectedRecords = this.getSelectionModel().getSelection();
		if(selectedRecords.length == 0) return;
		
		Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToDeleteSelectedSystem'),function(btn){
			if(btn == 'yes'){
				var ids = [];
				for ( var i = 0; i < selectedRecords.length; i++) {
					ids[i] = selectedRecords[i].get('id'); 
				}
				Ext.Ajax.request({
		            url:'pc/delete.do',
		            params: {
		            	ids : ids
		            },
		            scope : this,
		            success : function(result, request){
		                this.getStore().reload();
		            },
		            failure: Common.failureMessage
		        });  
			}
		},this);
	},
	
	search : function(){
		var searchCategory = this.queryById('searchCategory').getValue();
		var searchWordTxt = this.queryById('searchWordTxt').getValue();
		this.getStore().load({
			params : {
				searchCategory : searchCategory,
				searchWordTxt : searchWordTxt
			}
		});
	}
});